package com.example.rabbitspringboot.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.rabbitspringboot.mapper.OrderMapper;
import com.example.rabbitspringboot.pojo.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Authther: mrwu
 * @date:2021/5/13-05
 * @Description:com.example.rabbitspringboot.service
 */
@Slf4j
@Service
public class OrderService extends ServiceImpl<OrderMapper,Order> {

//    @Autowired
//    OrderMapper orderMapper;

    //监听死信队列，将对过期订单状态进行修改
    @RabbitListener(queues = "queue.order.dlx")
    public void listenOrderDlx(String message){
       log.info(message+"已过期");
       //查询订单状态，如果订单状态不是已支付，将订单状态修改为已取消
        Order order = baseMapper.selectById(message);
        if (order.getStatus()==0){
            order.setStatus(2);//修改订单状态为已支付
            baseMapper.updateById(order);
        }
    }
    @RabbitListener(queues = "queue.order")
    public void listenOrder(String message){
        //执行下单任务
        //插入一条订单
//        baseMapper.insert()
    }


    @Transactional
    public void payOrderById(String id){
        Order order = baseMapper.selectById(id);
        order.setStatus(1);//修改订单状态
        baseMapper.updateById(order);
    }



}
